#!/bin/bash  
  
Usage()  
{  
    echo "Usage: ${0##*/} [option] [path] filename"  
    echo "    -a,--absolute  dispaly absolute path"  
    echo "    -r,--relative  dispaly relative path"  
    echo "    -h,--help      display this information"  
    echo "[Note]: If you don't specify any option, -a will be used by default."  
}  
  
if [ $# -eq 0 ]; then  
    echo "Invalid usage: no filename specified!"  
    exit 1  
fi  
  
while [ $# -gt 0 ]; do  
    case $1 in  
    -*)  
        # ignore duplicated and conflicted options  
        case $1 in  
        -a|--absolute)  
            FLAG_ABSOLUTE_PATH="true"  
            ;;  
        -r|--relative)  
            FLAG_ABSOLUTE_PATH="false"  
            ;;  
        -h|--help)  
            Usage  
            exit 0  
            ;;  
        *)  
            echo "Invalid usage: unknown option $1!"  
            exit 1  
            ;;  
        esac ;;  
    *)  
        if [ -d "$1" ]; then  
            if [ -z "$SEARCH_PATH" ]; then  
                SEARCH_PATH="$1"  
            elif [ -z "$SEARCH_FILE" ]; then  
                SEARCH_FILE="$1"  
            else  
                echo "Invalid usage: too many directory parameters!"  
                exit 1  
            fi  
        elif [ -z "$SEARCH_FILE" ]; then  
            SEARCH_FILE="$1"  
        else  
            echo "Invalid use: too many filename parameters!"  
            exit 1  
        fi  
        ;;  
    esac  
    shift  
done  
  
if [ -z $FLAG_ABSOLUTE_PATH ]; then  
    FLAG_ABSOLUTE_PATH="true"  
fi  
  
if [ -z "$SEARCH_FILE" ]; then  
    # perhaps the filename to be searched is a path-style name, and not specify path parameter  
    if [ -n "$SEARCH_PATH" ]; then  
        SEARCH_FILE="$SEARCH_PATH"  
        SEARCH_PATH="."  
    else  
        echo "Invalid usage: no filename specified!"  
        exit 1  
    fi  
fi  
  
if [ -z "$SEARCH_PATH" ]; then  
  SEARCH_PATH="."  
fi  
  
SearchFile()  
{  
    # In some systems, find not support -L option (find: invalid predicate '-L')  
    if find -L /usr -maxdepth 1 -name tmp 2&> /dev/null ; then  
        FUN_SF_FIND_L_OPTION="-L"  
    else  
        FUN_SF_FIND_L_OPTION=""  
    fi  
    if [ "$FLAG_ABSOLUTE_PATH" = "true" ]; then  
        cd $SEARCH_PATH  
        FUN_SF_CURRENT_PATH=`pwd`/  
        find $FUN_SF_FIND_L_OPTION . -type f | grep $SEARCH_FILE | sed "s,\./,$FUN_SF_CURRENT_PATH,g" | grep --color $SEARCH_FILE  
    else  
        find $FUN_SF_FIND_L_OPTION $SEARCH_PATH -type f | grep --color $SEARCH_FILE  
    fi  
}  
  
SearchFile  
